package tv.youxing.android.ui;
/**
 * Created by apple on 2017/4/17.
 */

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import com.kaopiz.kprogresshud.KProgressHUD;
import com.xiasuhuei321.loadingdialog.view.LoadingDialog;

import java.util.ArrayList;
import java.util.List;

import tv.youxing.android.R;
import tv.youxing.android.widget.StatusBar.StatusBarCompat;

/**
 * 项目名：   youxing
 * 包名：     tv.youxing.android .ui
 * 文件名：   BaseActivity
 * 创建者：   叶克武
 * 创建时间： 2017/4/17 下午2:55
 * 描述：    TODO
 */
public class BaseActivity extends AppCompatActivity {





    protected List<Activity> mActivities = new ArrayList<>();
    private LoadingDialog mLoadingDialog;
    private KProgressHUD hud;




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //状态栏颜色
        StatusBarCompat.setStatusBarColor(this, ContextCompat.getColor(this, R.color.color_black));


        //显示返回键
//        getSupportActionBar().setDisplayHomeAsUpEnabled(false);

//        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
//                WindowManager.LayoutParams.FLAG_FULLSCREEN);
//
//        initWindow();
        //setStatusBarColor(this,R.color.black);

//        Window window = getWindow();
////取消设置透明状态栏,使 ContentView 内容不再覆盖状态栏
//        window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
//
////需要设置这个 flag 才能调用 setStatusBarColor 来设置状态栏颜色
//        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
////设置状态栏颜色
//        window.setStatusBarColor(R.color.black);
//
//        ViewGroup mContentView = (ViewGroup) findViewById(Window.ID_ANDROID_CONTENT);
//        View mChildView = mContentView.getChildAt(0);
//        if (mChildView != null) {
//            //注意不是设置 ContentView 的 FitsSystemWindows, 而是设置 ContentView 的第一个子 View . 预留出系统 View 的空间.
//            ViewCompat.setFitsSystemWindows(mChildView, true);
//        }


        //图片可以显示在状态栏下面， 原理 1.先设置全屏 2.然后设置显示标题栏
//        getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN);
//        getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);


        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    }


     void setStatusBarColor(Activity activity, int statusColor) {
        Window window = activity.getWindow();
        //取消状态栏透明
        window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        //添加Flag把状态栏设为可绘制模式
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        //设置状态栏颜色
        window.setStatusBarColor(statusColor);
        //设置系统状态栏处于可见状态
        window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
        //让view不根据系统窗口来调整自己的布局
        ViewGroup mContentView = (ViewGroup) window.findViewById(Window.ID_ANDROID_CONTENT);
        View mChildView = mContentView.getChildAt(0);
        if (mChildView != null) {
            ViewCompat.setFitsSystemWindows(mChildView, false);
            ViewCompat.requestApplyInsets(mChildView);
        }
    }



    protected void showDialog(Activity activity,boolean isShow) {
        if (mLoadingDialog == null) {
            mLoadingDialog = new LoadingDialog(activity);
        }
        if (mLoadingDialog != null) {
            mLoadingDialog.setLoadingText("加载中")
                    .setSuccessText("加载成功")//显示加载成功时的文字
                    //.setFailedText("加载失败")
                    .setInterceptBack(true)
                    .setLoadSpeed(LoadingDialog.Speed.SPEED_TWO)
                    .setRepeatCount(1);
        }
        if (isShow) {
            mLoadingDialog.show();
        } else {
            mLoadingDialog.close();
        }

    }


    public void showProgress(String message)
    {
        if(hud==null)
        {
            hud = KProgressHUD.create(this)
                    .setStyle(KProgressHUD.Style.SPIN_INDETERMINATE)
                    .setLabel(message)
                    .setCancellable(true);
        }
        if(!hud.isShowing())
        {
            hud.show();
        }
    }
    public void hideProgress()
    {
        if(hud != null && hud.isShowing())
        {
            hud.dismiss();
        }
    }


    //  菜单栏操作
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case android.R.id.home:
                finish();
                break;
        }
        return super.onOptionsItemSelected(item);
    }


    /**
     * 完成login进去主页面清除main之前所有的activity
     */
    public void removeLoginActivitys(){
        

    }




    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
